//
//  DDMusicClassViewController.m
//  DDMusic
//
//  Created by rimi on 16/11/10.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDMusicClassViewController.h"
#import "DDHongkongAndTaiwanController.h"
#import "DDInlandController.h"
#import "DDJpanController.h"
#import "DDHotmusicController.h"
#import "DDKoreaController.h"
#import "DDEuropeAndAmericaController.h"
#import "UIView+DDFnxtention.h"


@interface DDMusicClassViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIButton* currentSelectedButton;//当前选中按钮
@property(nonatomic,strong)UIView* indicatorView;
@property(nonatomic,strong)UIView* titleView;
@property(nonatomic,strong)UIScrollView* contentView;
@end

@implementation DDMusicClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addSubController];
    [self setUpTopTitlesView];
    [self setContentView];
}

- (void)addSubController{
    DDInlandController* inlandVc = [[DDInlandController alloc]init];
    [self addChildViewController:inlandVc];
    DDHongkongAndTaiwanController* hongkongVc = [[DDHongkongAndTaiwanController alloc]init];
    [self addChildViewController:hongkongVc];
    DDKoreaController* koreaVc = [[DDKoreaController alloc]init];
    [self addChildViewController:koreaVc];
    DDJpanController* japanVc = [[DDJpanController alloc]init];
    [self addChildViewController:japanVc];
    DDEuropeAndAmericaController* europeVc = [[DDEuropeAndAmericaController alloc]init];
    [self addChildViewController:europeVc];
    DDHotmusicController* hotVc = [[DDHotmusicController alloc]init];
    [self addChildViewController:hotVc];
}

- (void)setUpTopTitlesView{
    UIView* titlesView = [[UIView alloc]init];
    titlesView.backgroundColor = [UIColor colorWithRed:34/255.0 green:30/255.0 blue:29/255.0 alpha:1.0];
    titlesView.width = self.view.width;
    titlesView.height = 35;
    titlesView.y = 0;
    [self.view addSubview:titlesView];
    self.titleView = titlesView;
    UIView* indicatorView1 = [[UIView alloc]init];
    indicatorView1.backgroundColor = [UIColor redColor];
    indicatorView1.height = 2;
    indicatorView1.y = titlesView.height - indicatorView1.height;
    [titlesView addSubview:indicatorView1];
    self.indicatorView = indicatorView1;
    
    //内部子标签
    NSArray* titles = @[@"内地",@"港台",@"韩国",@"日本",@"欧美",@"热榜"];
    for (NSInteger i = 0; i < titles.count; i++) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.height = titlesView.height;
        button.width = titlesView.width/titles.count;
        button.x = i*button.width;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        if (i == 0) {
            button.enabled = NO;
            self.currentSelectedButton = button;
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
        }
        
    }
    [titlesView addSubview:indicatorView1];
}

-(void)titleClick:(UIButton *)button{
    //修改按钮状态
    self.currentSelectedButton.enabled = YES;
    button.enabled = NO;
    self.currentSelectedButton = button;
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    //滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag* self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
    
}

#pragma mark - 设置底部滚动视图
- (void)setContentView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView* contentView = [[UIScrollView alloc]init];
    contentView.delegate = self;
    contentView.frame = self.view.bounds;
    contentView.backgroundColor = [UIColor grayColor];
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(contentView.width* self.childViewControllers.count, 0);
    [self.view insertSubview:contentView atIndex:0];
    self.contentView = contentView;
    [self scrollViewDidEndScrollingAnimation:contentView];
}

#pragma mark - scrollview代理实现
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/scrollView.width;
    //取出子控制器
    UITableViewController* vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0;
    vc.view.height = scrollView.height;
    //设置内边距
    CGFloat botoom = 49.0f;
    CGFloat top = CGRectGetMaxY(self.titleView.frame);
    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, botoom, 0);
    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x/scrollView.width;
    [self titleClick:self.titleView.subviews[index]];
}

@end
